﻿package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	[SWF(width="636", height="464")]
	 
	public class PulseCollision extends Sprite {


		public var center_init: Number;
		public var center: Number;
		public var spread: Number;
		public var color: uint;
		private var wave_x: Number;
		private var wave_y: Number;
		private var delta_x: Number;
		private var wave_1_y: Number;
		private var wave_2_y: Number;
		private var reflector: Number;
		private var speed: Number = 4;
		private var i: uint = 0;
		private var alph: Number;
		private var ampl_1: Number;
		private var ampl_2: Number;
		private var controlpanel:ControlPanel;
		private var togglebutton:ToggleButton;
		private var amplslider:HorizontalSlider;
		private var yaxis1:Number = 200;
		private var yaxis2:Number = 300;
		private var waveylast:Number;


		public function PulseCollision(center:Number = 100, spread:Number = 20, color:uint=0xff0000) {
			this.center_init = center_init;
			this.center = center;
			this.spread = spread;
			this.color = color;
			this.alph = alph;
			this.ampl_2 = ampl_2;

			init();
		}
		public function init():void {

//			stage.scaleMode = StageScaleMode.NO_SCALE;
//			stage.align = StageAlign.TOP_LEFT;
			spread = 30;
			center_init = 100;
			center = center_init;
			wave_x = 10;
			delta_x = 2;
			reflector = 0.5*stage.stageWidth;
			alph = 1;
			ampl_1 = 60;
			ampl_2 = 60;
			controlpanel = new ControlPanel(stage.stageHeight, stage.stageWidth);
			addChild(controlpanel);
			amplslider = new HorizontalSlider(-1*ampl_2,ampl_2,-1*ampl_2,5,false);
			amplslider.x = .3*stage.stageWidth;
			amplslider.y = .91*stage.stageHeight;
			amplslider.height=.077*stage.stageHeight;
			addChild(amplslider);
			togglebutton = new ToggleButton(stage.stageHeight);
			addChild(togglebutton);
			togglebutton.x = .9*stage.stageWidth;
			togglebutton.y = .91*stage.stageHeight;
			addEventListener(Event.ENTER_FRAME, onEnterFrame);

		}
		public function onEnterFrame(event:Event):void {
			ampl_2 = amplslider.value;
			if (center < stage.stageWidth - center_init) {
				if(!controlpanel.pause) {center += speed;}
				if(controlpanel.stepback) {center -= speed/2;}
				if(controlpanel.stepforward) {center += speed/2;}
				if(controlpanel.reset) {
					center = center_init; 
					wave_x = 10;
					controlpanel.pause = true;
				}

				graphics.clear();
				drawwaves(yaxis1,ampl_2, 1);

				
			}
			else {
				controlpanel.pause = true;
				if(controlpanel.reset) {
					center = center_init; 
					wave_x = 10;
				}
			}
		}
		private function wavey(amp:Number,wavex:Number,center:Number,spread:Number):Number {
			return (amp*Math.exp(-((wavex - center)*(wavex - center)/(spread*spread))));
		}
		private function drawwaves(yaxis:Number, ampl2:Number, visible:Number):void {
			graphics.lineStyle(3, 0x000000, 1 - alph);
			graphics.moveTo(reflector, yaxis-90);
			graphics.lineTo(reflector, yaxis+90);

			graphics.lineStyle(1, 0x000000, 0);
			graphics.moveTo(reflector*2, yaxis);
			graphics.lineTo(0, yaxis);
			if(!togglebutton.up){
			for (i = 0; i < (2*reflector/delta_x); i++) {
				wave_x =  i*delta_x;
				wave_y = yaxis + wave_1_y + wave_2_y;

				wave_1_y = wavey(-1*ampl_1,wave_x,center,spread);
				wave_2_y = wavey(ampl2,wave_x,2*reflector-center,spread);

				if (i < (reflector/delta_x)) {
					graphics.lineStyle(3, 0x00ff00, 1*visible);
				} else {
					graphics.lineStyle(3, 0x00ff00, alph*visible);
				}
				graphics.lineTo(wave_x, yaxis + wave_1_y);
			}
			graphics.moveTo(0,yaxis);

			for (i = 0; i < (2*reflector/delta_x); i++) {
				wave_x =  i*delta_x;
				wave_y =  wave_1_y + wave_2_y;

				wave_1_y = wavey(-1*ampl_1,wave_x,center,spread);
				wave_2_y = wavey(ampl2,wave_x,2*reflector-center,spread);

				if (i < (reflector/delta_x)) {
					graphics.lineStyle(3, 0xff0000, 1*visible);
				} else {
					graphics.lineStyle(3, 0xff0000, alph*visible);
				}
				graphics.lineTo(wave_x, yaxis + wave_2_y);
			}
			}
			graphics.moveTo(0,yaxis);

			for (i = 0; i < (2*reflector/delta_x); i++) {
				wave_x =  i*delta_x;

				wave_1_y = wavey(-1*ampl_1,wave_x,center,spread);
				wave_2_y = wavey(ampl2,wave_x,2*reflector-center,spread);
				wave_y =  wave_1_y + wave_2_y;

				if (i < (reflector/delta_x)) {
					graphics.lineStyle(1.5, 0x000000, 1);
				} else {
					graphics.lineStyle(1.5, 0x000000, alph*visible);
				}
				graphics.lineTo(wave_x, yaxis + wave_y);
			}
			graphics.moveTo(0,yaxis);

		}
	}
}